﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace lib.dmt.teltonika.server.Parsers
{
    public static class TeltonikaAVLParametersID
    {
        private static Dictionary<int, string>? dict = null;

        public static string GetName(int id)
        {
            if (dict == null)
            {
                InitAvlIds();
            }
            if (dict!.ContainsKey(id))
            {
                return dict[id];
            }
            else
            {
                return string.Empty;
            }
        }

        private static void InitAvlIds(string file = "./avl_ids.txt")
        {
            if (File.Exists(file) == false)
            {
                throw new FileNotFoundException(file);
            }
            dict = new Dictionary<int, string>();

            string[] lines = File.ReadAllLines(file);
            foreach (string line in lines)
            {
                string[] items = line.Split(new char[] { ';' });
                if (items.Length == 2)
                {
                    if (dict.ContainsKey(int.Parse(items[0])))
                    {
                        dict[int.Parse(items[0])] = items[1];
                    }
                    else
                    {
                        dict.Add(int.Parse(items[0]), items[1]);
                    }
                }
            }
        }
    }
}
